#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
#define MAX_SIZE 1000
struct ListNode
{
    int data;
    ListNode* link;
};

void CreatList(ListNode* list, int nums[], int n) {
    ListNode* l1 = list;
    for (int i = 0; i < n; ++i) {
        ListNode* node = new ListNode;
        node->data = nums[i];
        node->link = nullptr;
        l1->link = node;
        l1 = l1->link;
    }
}
void PrintList(ListNode* list) {
    ListNode* l1 = list->link;
    while (l1) {
        cout << l1->data << " ";
        l1 = l1->link;
    }
}
ListNode* DeleteNode(ListNode* list) {
    int count[MAX_SIZE + 1] = { 0 };
    ListNode* pre = list, * p = list->link;
    while (p) {                     //遍历链表，当p为空（结尾节点的下一个）时结束
        if (!count[abs(p->data)]) { //之前未出现相等的绝对值
            count[abs(p->data)]++;  //计数器加一
            pre = pre->link;        //pre向后移动一个节点
        }
        else {
            ListNode* q = p;
            pre->link = p->link;    //pre跳过重复节点
            delete q;               //删除重复节点
        }
        p = pre->link;              //p为pre的下一个节点
    }
    return list;
}

int main() {
    ListNode* list = new ListNode;
    int nums[5] = { 21, -15, -15, -7, 15 };
    CreatList(list, nums, 5);
    PrintList(list);

    list = DeleteNode(list);
    cout << endl;
    PrintList(list);
    return 0;
}